/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {

public:
    ListNode* reverseBetween(ListNode* head, int left, int right) {
        
        ListNode tmp;
        ListNode* dumy=&tmp;
        dumy->next=head;
        ListNode* p1=head,*p2=head;
        while(p1 && left-->1){
            p1=p1->next;
            dumy=dumy->next;
        }
        while(p2 && right-->1){
            p2=p2->next;
        }
        ListNode* pre=p2->next;
        ListNode* cur=p1,*tail=p2->next;
        while(cur!=tail){
            ListNode* nextNode=cur->next;
            cur->next=pre;
            pre=cur;
            cur=nextNode;
        }
        dumy->next=pre;
        return tmp.next;
    }
};
